package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.OrdersettingMapper;
import com.qf.pojo.OrderSetting;
import com.qf.service.OrdersettingService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class OrdersettingServiceImpl extends ServiceImpl<OrdersettingMapper, OrderSetting> implements OrdersettingService {

    @Override
    public List<OrderSetting> findByMonth(String beginDay, String endDay) {
        OrdersettingMapper ordersettingMapper = getBaseMapper();

        LambdaQueryWrapper<OrderSetting> qw = new LambdaQueryWrapper<>();
        qw.between(OrderSetting::getOrderDate,beginDay,endDay);
        List<OrderSetting> orderSettings = ordersettingMapper.selectList(qw);

        return orderSettings;
    }

    @Override
    public void addOrUpdate(OrderSetting orderSetting) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        OrdersettingMapper ordersettingMapper = getBaseMapper();

        LambdaQueryWrapper<OrderSetting> qw = new LambdaQueryWrapper<>();
        qw.eq(OrderSetting::getOrderDate,sdf.format(orderSetting.getOrderDate()));

        OrderSetting os = ordersettingMapper.selectOne(qw);
        if (os != null){
            ordersettingMapper.update(orderSetting,qw);
        }else {
            ordersettingMapper.insert(orderSetting);
        }
    }


}
